#include <iostream>
#include <vector>
using namespace std;



////////////////////////// WORKING CORRECTLY //////////////////////////////////////
void swapinteger(int* a, int* b){
     int swap = *a;
     *a = *b;
     *b = swap;
}

//////////////////// WORKING CORRECTLY ///////////////////////////////

int gcd(int a, int b){
    if (a < b) swapinteger(&a, &b);
    int remainder = a % b;
 
    if (remainder == 0) return b;
    else return gcd(b, remainder);
}

//////////////////////////////////////////////////////////////////////
        // Author: Md. Mohsin Ali
void FactorRefinement(vector<int>& R)
        {//---------------------------------------------------  
            vector<int>::iterator itn1;
            vector<int>::iterator itn2, temp1, temp2;

            int n1_d;  
            int n2_d;  
            int d, e1, e2;


            itn1 = R.begin();
                  
         
            while(itn1 != R.end())
               {
                itn2 = itn1;
                ++itn2; // factor
                ++itn2; // exponent

               while(itn2 != R.end())
                    {    
                        d = gcd(*itn1, *itn2); 

                     if(d == 1)
                        {
                         ++itn2;  ++itn2;
                        }
		     else if(d != 1)
                        {
                        n1_d = *itn1/d;
                        n2_d = *itn2/d;

                        e1 = *(itn1+1); 
                        e2 = *(itn2+1); 

		        if(n1_d != 1)
                          {   
                           R.push_back(n1_d);
                           R.push_back(e1);                 
                          }
                        
                        R.push_back(d);
                        R.push_back(e1 + e2);

  
                        if(n2_d != 1)
                          {
                           R.push_back(n2_d);
                           R.push_back(e2);
                          }
			cout << "*itn1: " << *itn1 << endl;  
			R.erase(itn2, itn2 + 2);
			cout << "*itn1: " << *itn1 << endl;  
			R.erase(itn1, itn1 + 2);

                        itn1 = R.begin(); 
                        itn2 = itn1 + 2;
                        }//end of else

                    }//end of inner while

                if(d == 1)
                  {
                  ++itn1;
                  ++itn1;
                  }
               }//end of while
		  cout <<"R size:" << R.size() << endl;
        }      

//////////////////////////////////////////////////////////////////////////////////////

int main()
{
vector<int> A;
vector<int> B;
vector<int>& C=A;
   {
   A.push_back((i+1)*16);
   A.push_back(1);
   }

FactorRefinement(C);

		  cout <<"C size: " << C.size() << endl;
		  cout <<"A size: " << A.size() << endl;

vector<int>::iterator i, j;

for(i = C.begin(); i != C.end(); ++++i)
   {
   j = ++i;
   --i; 
   cout<<"("<<*i<<","<<*j<<")";
   if(++j != C.end()) cout<<", ";
   }

}
